package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Communities;
import com.xxx.entity.dto.CommunitiesDto;
import com.xxx.service.ICommunitiesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 社区信息表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/communities")
@Tag(name = "社区信息表")
public class CommunitiesController {

    @Autowired
    private ICommunitiesService communitiesService;

    @PostMapping("/add")
    @Operation(summary = "添加社区")
    public Result add(@RequestBody Communities communities){
        Boolean flag = communitiesService.save(communities);
        return flag ? Result.success("添加社区成功") : Result.error("添加社区失败");
    }

    @GetMapping("/delete/{communityId}")
    @Operation(summary = "删除社区")
    public Result delete(@PathVariable Integer communityId){
        Boolean flag = communitiesService.removeById(communityId);
        return flag ? Result.success("删除社区成功") : Result.error("不存在该社区Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改社区")
    public Result update(@RequestBody Communities communities){
        Boolean flag = communitiesService.updateById(communities);
        return flag ? Result.success("修改社区成功") : Result.error("修改社区失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询社区")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Communities> page = communitiesService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据社区Id、社区名、社区位置条件查询社区")
    public Result selectByCondition(@RequestBody CommunitiesDto communitiesDto){
        LambdaQueryWrapper<Communities> lqw = new LambdaQueryWrapper<>();
        lqw.like(communitiesDto.getCommunityName()!=null&&!communitiesDto.getCommunityName().equals(""),
                        Communities::getCommunityName,communitiesDto.getCommunityName())
                .like(communitiesDto.getCommunityLocation()!=null&&!communitiesDto.getCommunityLocation().equals(""),
                        Communities::getCommunityLocation,communitiesDto.getCommunityLocation())
                .eq(communitiesDto.getCommunityId()!=null,Communities::getCommunityId,communitiesDto.getCommunityId());
        IPage<Communities> page = communitiesService.page(new Page<>(communitiesDto.getCurrent(), communitiesDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

